//
//  DDWelcomeViewControllor.swift
//  swift微博
//
//  Created by whitcast on 16/7/13.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class DDWelcomeViewControllor: UIViewController {
    //头像
    lazy var headImgView : UIImageView = {
        
    let imgView = UIImageView()
        
        let model = DDUserAccountModel.getDataFormLocal()
        
//        imgView.sd_setImageWithURL(NSURL(string: model.profile_image_url!))
        
        imgView.sd_setImageWithURL(NSURL(string: (model?.profile_image_url!)!), placeholderImage: UIImage(named: "avatar_default_big"))
//       imgView.image = UIImage(named: "avatar_default_big")
        
//        imgView.layer.cornerRadius = 45  //使用上传的图片会有不圆的现象
        
        imgView.layer.masksToBounds = true
    
        return imgView
    }()
    //名字
    lazy var nameLab : UILabel = {
     let lab = UILabel()
        //获取本地用户数据
        let model = DDUserAccountModel.getDataFormLocal()
        
        let name = model?.screen_name
        
        lab.text = "欢迎登录: "+"\(name!)"
        
        lab.font = UIFont.systemFontOfSize(14)
        
        lab.textColor = UIColor.grayColor()
    
        return lab
    
    }()
    //背景图片
    lazy var backImgView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }
    
    //创建UI
    func setUpUI() {
        //添加控件
        view.addSubview(backImgView)
        
        view.addSubview(headImgView)
        
        view.addSubview(nameLab)
        //设置约束
        backImgView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self.view)
            make.trailing.equalTo(self.view)
            make.top.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
        
        headImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view).offset(50)
        }
        
        nameLab.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(headImgView.snp_bottom).offset(20)
        }
        
    }

    //开启动画
    func startAnimation() {
        headImgView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        //第一个参数 持续时间
        //第二个 延迟执行时间
        //第三个:阻尼效应,越大弹簧效果越小
        //第四个:初始速度
        //第五个: []
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //进入首页
                NSNotificationCenter.defaultCenter().postNotificationName("changeVC", object: nil)
        }
        
    }
}
